
function GPWidget() {

	this.debug = false;
	this.j = null;	
	this.items = null;
	this.widget = null;
	this.view = 'blurb';
	this.title = "World";
	this.offset = 0;
	this.limit = 3;
	this.total = 10;
	this.interval = null;
	
	this.initialize = function() {
		if (this.view == 'headline') {
			this.limit = 6;
		} else if (this.view == 'story') {
			this.limit = 1;
		}
		this.log('initialize');
		this.j = jQuery.noConflict(true);
		this.j.getJSON('http://globalpostweb.com/widgets/publisher/data.php?callback=?', {'f': '1|2|3|5|9', 'view': 'blurb'},
			function(data) { 
				GPWIDGET.items = data; 
				GPWIDGET.show_data();
		});
	}
	
	this.load = function(t, opts) {
		var e = document.createElement(t);
        for (var i in opts) {
            e.setAttribute(i, opts[i]);
        }
        try {
            document.getElementsByTagName('head')[0].appendChild(e);
        } catch (err) {
            document.body.appendChild(e);
        }
    }
	
	this.create_headline = function(title, content, url, i) {
		var cycle = (i % 2 == 0) ? 'even' : 'odd';
		var div = this.j(document.createElement('div')).addClass('gpitem ' + cycle);
		div.append(
			this.j(document.createElement('h6')).append(
				this.j(document.createElement('a')).attr('href', url).attr('target', '_blank').html(title)
			)
		);
		if (this.view != 'headline') {
			div.append(this.j(document.createElement('div')).addClass('gpcontent').html(content));
		}
		this.widget.append(div);
	}
	
	this.create_header = function() {
		var div = this.j(document.createElement('div')).addClass('gpheader');
		div.html('<div>' + this.title + '</div>');
		this.j(div).bind('click', function(e) { window.open('http://www.globalpost.com/') });
		this.widget.append(div);
	}
	
	this.create_bar = function() {
		var div = this.j(document.createElement('div')).addClass('gpbar');
		this.widget.append(div);
	}
	
	this.create_pager = function() {
		var div = this.j(document.createElement('div')).addClass('gppager');
		if (this.offset > 0) {
			div.append(this.j(document.createElement('a')).attr('href', '#').addClass('previous').text('previous').bind('click', function(e) { e.preventDefault(); GPWIDGET.change_page('previous'); }));
		}
		if ((this.offset + this.limit) < this.total) {
			div.append(this.j(document.createElement('a')).attr('href', '#').addClass('next').text('next').bind('click', function(e) { e.preventDefault(); GPWIDGET.change_page('next'); }));
		}
		this.widget.append(div);
		this.log('create_pager');
	}
	
	this.change_page = function(direction) {
		if (direction == 'next') {
			this.offset += this.limit;
		} else if (direction == 'previous') {
			this.offset -= this.limit;
		}
		this.log('change_page');
		this.show_data();
	}
	
	this.show_data = function() {
		if (this.widget == null) this.widget = this.j('#gpwidget');
		this.total = this.items.length;
		this.widget.empty();
		this.create_bar();
		this.create_header();
		this.create_pager();
		var max = this.limit + this.offset;
		if (max > this.total) max = this.total;
		for (var i = this.offset; i < max; i++) {
			var item = this.items[i];
			this.create_headline(item.title, item.content, item.url, i)
		}
		this.log('show_data');
		this.create_bar();
	}
	
	this.log = function(message) {
		if (this.debug) {
			window.console ? window.console.log(message) : alert(message);
		}
	}
	
	this.check_jquery = function() {
		if (typeof(jQuery) == 'undefined') {
			return false;
		} else {
			clearInterval(this.interval);
			this.checker = null;
			this.initialize();
			return true;
		}
	}
	
	
	if (typeof(jQuery) != 'undefined' && jQuery.fn.jquery >= '1.2.6') {	
		this.log('jQuery already loaded');
		this.check_jquery();
	} else {
		this.load('script', {
            src: 'http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js',
            type: 'text/javascript'
        });
		this.interval = setInterval('GPWIDGET.check_jquery()', 100);
		this.log('jQuery loading...');
	}
	this.load('link', {
		href: 'http://globalpostweb.com/widgets/publisher/style.css',
        rel: 'stylesheet',
        type: 'text/css'
    });
	document.write('<div id="gpwidget" class="blurb">Loading...</div>');
	
}

var GPWIDGET = {};
GPWIDGET = new GPWidget();